#include"OpenGLCGI.h"
#include"Runtime/Debug/Logger.h"
#ifdef BATTLEFIRE_WIN_PLAYER
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")
#endif

namespace BattleFire {
	PFNGLDISCARDFRAMEBUFFEREXT OpenGLCGI::DiscardFramebuffer = nullptr;
	PFNGLDISPATCHCOMPUTE OpenGLCGI::DispatchCompute = nullptr;
	PFNPUSHGROUPMARKER OpenGLCGI::PushGroupMarker = nullptr;
	PFNPOPGROUPMARKER OpenGLCGI::PopGroupMarker = nullptr;
	PFNINSERTEVENTMARKER OpenGLCGI::InsertEventMarker = nullptr;
	bool CGI::mbSupportFloatColorBuffer = false;
	bool CGI::mbSupportHalfFloatColorBuffer = false;
	bool CGI::mbSupportHalfFloatVertex = false;
	bool CGI::mbSupportComputeShader = false;

	int CGI::mMaxVertexShaderSSBOCount = 0;
	int CGI::mMaxTessellaionControlShaderSSBOCount = 0;
	int CGI::mMaxTessellaionEvaluationShaderSSBOCount = 0;
	int CGI::mMaxGeometryShaderSSBOCount = 0;
	int CGI::mMaxFragmentShaderSSBOCount = 0;
	int CGI::mMaxComputeShaderSSBOCount = 0;
	int CGI::mMaxCombinedShaderSSBOCount = 0;
	bool OpenGLCGI::Initialize(HDC inHDC,int inViewWidth,int inViewHeight) {
		
		PIXELFORMATDESCRIPTOR pfd;
		ZeroMemory(&pfd, sizeof(pfd));

		pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
		pfd.nVersion = 1;
		pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_TYPE_RGBA;
		pfd.iPixelType = PFD_TYPE_RGBA;
		pfd.cColorBits = 32; // 8 bits stencil buffer
		pfd.cDepthBits = 24; // 24 bits zbuffer
		pfd.cStencilBits = 8; // 8 bits stencil buffer
		pfd.iLayerType = PFD_MAIN_PLANE; // main layer
		int pixelformat = ChoosePixelFormat(inHDC, &pfd);
		if (pixelformat == 0)
		{
			Error("choose pixel format fail %d", pixelformat);
			return false;
		}

		if (SetPixelFormat(inHDC, pixelformat, &pfd) == FALSE)
		{
			Error("pixel format is %d", pixelformat);
			return false;
		}
		wglMakeCurrent(NULL, NULL);
		HGLRC rc = wglCreateContext(inHDC);
		if (rc == NULL)
		{
			Error("create GL RC fail!\n");
			return false;
		}
		if (!wglMakeCurrent(inHDC, rc))
		{
			Error("GLContext: failed to activate context\n");
			return false;
		}
	}
}